import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
// 修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    //重定向登录页
    path: "/",
    redirect: "/login2",
  },
  {
    //重定向登录页
    path: "/home2",
    name: "home2",
    component: () => import("../views/home/home2.vue"),
    children: [
      {
        //主页
        path: "/homepage",
        name: "homepage",
        component: (resolve) =>
          require(["../views/home2/homepage.vue"], resolve),
      },
      {
        //重要事项提醒
        path: "/warn",
        name: "warn",
        component: () => import("../views/home2/warn.vue"),
      },
      {
        //未读报表
        path: "/unreadTable",
        name: "unreadTable",
        component: () => import("../views/home2/unreadTable.vue"),
      },
      {
        //最近浏览
        path: "/recently",
        name: "recently",
        component: () => import("../views/home2/recently.vue"),
      },
      {
        //可视化报表
        path: "/visualization",
        name: "visualization",
        component: () => import("../views/home2/visualization.vue"),
      },
      {
        path: "/home",
        name: "home",
        component: () => import("../views/home/index.vue"),
      },
      {
        //利润表
        path: "/profitTable",
        name: "profitTable",
        component: () =>
          import("../views/threeTableCopy/profitTable/profitTable.vue"),
      },
      {
        //资产负债表
        path: "/balanceTable",
        name: "balanceTable",
        component: () =>
          import("../views/threeTableCopy/balanceTable/balanceTable.vue"),
      },
      {
        //现金流量表
        path: "/cashTable",
        name: "cashTable",
        component: () =>
          import("../views/threeTableCopy/cashflowTable/cashflowTable.vue"),
      },
      {
        //应收款表
        path: "/accountReceivable",
        name: "accountReceivable",
        component: () => import("../views/threeTable/accountReceivable.vue"),
      },

      {
        //库存
        path: "/inventory",
        name: "inventory",
        component: () => import("../views/threeTable/inventory.vue"),
      },
      {
        //临时报表
        path: "/forTable",
        name: "forTable",
        component: () => import("../views/threeTable/forTable.vue"),
      },
      {
        //重要提醒
        path: "/important",
        name: "important",
        component: () => import("../views/threeTable/important.vue"),
      },

      {
        //盈利主要指标分析
        path: "/profit",
        name: "profit",
        component: () => import("../views/analyse/profit.vue"),
      },

      {
        //research 研发部
        path: "/research",
        name: "research",
        component: () => import("../views/research/research.vue"),
      },
      {
        //productions 生产部
        path: "/productions",
        name: "productions",
        component: () => import("../views/productions/productions.vue"),
      },
      {
        //purchase 采购部
        path: "/purchase",
        name: "purchase",
        component: () => import("../views/purchase/purchase.vue"),
      },
      {
        //warehouse 仓库部
        path: "/warehouse",
        name: "warehouse",
        component: () => import("../views/warehouse/warehouse.vue"),
      },
      {
        //market 销售部
        path: "/market",
        name: "market",
        component: () => import("../views/market/market.vue"),
      },
      {
        //personnel人事部
        path: "/personnel",
        name: "personnel",
        component: () => import("../views/personnel/personnel.vue"),
      },

      {
        //在线编辑
        path: "/onlineEdit",
        name: "onlineEdit",
        component: () => import("../views/onlineEdit/onlineEdit.vue"),
      },
      {
        //账户设置
        path: "/userset",
        name: "userset",
        component: () => import("../views/userset/userset.vue"),
      },
    ],
  },
  {
    //首页
    path: "/homes",
    name: "homes",

    component: () => import("../views/home/home.vue"),
    // children: [
    //   {
    //     path: "/home",
    //     name: "home",
    //     component: () => import("../views/home/index.vue"),
    //   },
    //   {
    //     //利润表
    //     path: "/profitTable",
    //     name: "profitTable",
    //     component: () => import("../views/threeTable/profitTable.vue"),
    //   },
    //   {
    //     //资产负债表
    //     path: "/balanceTable",
    //     name: "balanceTable",
    //     component: () => import("../views/threeTable/balanceTable.vue"),
    //   },
    //   {
    //     //现金流量表
    //     path: "/cashTable",
    //     name: "cashTable",
    //     component: () => import("../views/threeTable/cashTable.vue"),
    //   },
    //   {
    //     //应收款表
    //     path: "/accountReceivable",
    //     name: "accountReceivable",
    //     component: () => import("../views/threeTable/accountReceivable.vue"),
    //   },

    //   {
    //     //库存
    //     path: "/inventory",
    //     name: "inventory",
    //     component: () => import("../views/threeTable/inventory.vue"),
    //   },
    //   {
    //     //临时报表
    //     path: "/forTable",
    //     name: "forTable",
    //     component: () => import("../views/threeTable/forTable.vue"),
    //   },
    //   {
    //     //重要提醒
    //     path: "/important",
    //     name: "important",
    //     component: () => import("../views/threeTable/important.vue"),
    //   },

    //   {
    //     //盈利主要指标分析
    //     path: "/profit",
    //     name: "profit",
    //     component: () => import("../views/analyse/profit.vue"),
    //   },
    // ],
  },
  {
    //登录页
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
  {
    //登录页
    path: "/login2",
    name: "login2",
    component: (resolve) => require(["@/views/login2.vue"], resolve),
  },
];

const router = new VueRouter({
  routes,
});

// 路由导航守卫      访问的白名单, 可以直接通行
const whiteList = ["/login2"];

router.beforeEach((to, from, next) => {
  // 访问的路径在白名单
  if (whiteList.includes(to.path.toLowerCase())) {
    next();
  } else {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/login2");
    }
  }
});

export default router;
